<?php

namespace PHPSpring;

use PHPSpring\Annotation\Impl\Controller;
use PHPSpring\Utils\File;
use PHPSpring\Utils\Scanner;
use PHPSpring\Core\AnnotationFactory;
use PHPSpring\Core\RuntimeAnnotation;

class Application {
  private static string $rootPath;

  public static function init ($projectPath=null) {
    self::$rootPath = $projectPath;
    RuntimeAnnotation::init($projectPath);
  }

  public static function initRoutes ($callback) {
    Controller::init($callback);
  }

  public static function scan (string $path, string $projectPath, $classPrefix='') {
    Scanner::scan($path, $projectPath, $classPrefix);
  }

  public static function registerAnnotation (string $annotationFullName) {
    AnnotationFactory::register($annotationFullName);
  }

  public static function rootPath ($sub = '', $postfix = false): string {
    if (!empty(self::$rootPath)) {
      $rootPath = self::$rootPath;
    } elseif (defined('ROOT_PATH')) {
      $rootPath = ROOT_PATH;
    } else {
      $rootPath = dirname(__DIR__, 4); //Not working in win32 symlink, need define ROOT_PATH first.
    }

    return File::unifyPath($rootPath.'/'.ltrim($sub, '/\\'), $postfix, false);
  }

  public static function appPath ($sub = '', $postfix = false, $appFolder = 'app'): string {
    return self::rootPath(trim($appFolder, '/\\').'/'.ltrim($sub, '/\\'), $postfix);
  }

  public static function config (string $key, $default = null) {
    //TODO
    return config($key, $default);
  }

}
